package com.capgemini.gae;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.capgemini.gae.daos.OlympicAdressDao;
import com.capgemini.gae.daos.PerformanceDao;
import com.capgemini.gae.daos.PersonDao;
import com.capgemini.gae.daos.TournamentDao;
import com.capgemini.gae.data.Athlete;
import com.capgemini.gae.data.Discipline;
import com.capgemini.gae.data.OlympicAdress;
import com.capgemini.gae.data.Performance;
import com.capgemini.gae.data.Tournament;
import com.capgemini.gae.data.Trainer;
import com.google.appengine.api.datastore.Key;

@SuppressWarnings("serial")
public class Olympic_gaeServlet extends HttpServlet {
	@SuppressWarnings("deprecation")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		StringBuilder builder = new StringBuilder();

		TournamentDao tourdao = new TournamentDao();
		PerformanceDao perfdao = new PerformanceDao();

		Tournament memorial = new Tournament("Diegems Gemeentelijke Marathon",
				"Diegem", 2, new Date(112, 6, 24), new Date(112, 6, 24),
				Discipline.MARATHON);
		memorial = tourdao.save(memorial);
		Performance perf1 = new Performance(new Date(123456789), 3, false,
				memorial);
		Performance perf2 = new Performance(new Date(223456789), 5, false,
				memorial);
		memorial.getPerformances().add(perf1);
		memorial.getPerformances().add(perf2);
		memorial = tourdao.save(memorial);
		memorial = tourdao.load(Tournament.class, memorial.getKey());
		Key keyperf = memorial.getPerformances().iterator().next().getKey();
		Performance temp = perfdao.load(Performance.class, keyperf);

		memorial.setEdition(55);
		memorial = tourdao.save(memorial);
		memorial = tourdao.load(Tournament.class, memorial.getKey());
		keyperf = memorial.getPerformances().iterator().next().getKey();

		temp = perfdao.load(Performance.class, keyperf);
		builder.append(temp.getRanking() + "\n");
		Key keytour = memorial.getKey();
		Tournament temptour = tourdao.load(Tournament.class, keytour);
		builder.append(temptour.getPerformances().size() + "\n");

		// Person-Trainer-Athlete test
		Athlete jef = new Athlete("Jef", "De Neve", new java.util.Date(89, 8,
				17), "Belgium", Discipline.TRIATHLON, 666);
		PersonDao persdao = new PersonDao();
		jef = (Athlete) persdao.save(jef);
		Key tempid = jef.getKey();
		jef = (Athlete) persdao.load(Athlete.class, tempid);
		builder.append(jef + "\n");
		jef.addDopingHistory("2004: EPO");
		jef.addDopingHistory("2010: Blood transfusion");
		jef = (Athlete) persdao.save(jef);
		jef.getDopingHistory().remove(0);
		jef = (Athlete) persdao.save(jef);
		Trainer leekens = new Trainer("George", "Leekens", new java.util.Date(
				66, 3, 1), "Belgium", true, "main coach");
		leekens = (Trainer) persdao.save(leekens);

		// adding OlympicAdress
		OlympicAdressDao oadao = new OlympicAdressDao();
		OlympicAdress address1 = new OlympicAdress("Bessenveldstraat", 91,
				"Opwijk");
		address1.setStreetNumber(19);
		address1.setCity("Diegem");
		jef.setAdress(address1);
		jef = (Athlete) persdao.save(jef);
		Key tempid3 = jef.getAdress().getKey();
		address1 = oadao.load(OlympicAdress.class, tempid3);
		builder.append(address1 + "\n");

		jef.addPerformance(perf1);
		jef = (Athlete) persdao.save(jef);
		jef = (Athlete) persdao.load(Athlete.class, tempid);
		perf1 = perfdao.load(Performance.class, keyperf);

		builder.append("\n\n\n" + perf1.getAthlete() + "\n");
		builder.append(jef + "\n");

		// Link Trainer-Athlete
		leekens.addAthletes(jef);
		leekens = (Trainer) persdao.save(leekens);
		Key temptrain = leekens.getKey();
		jef = (Athlete) persdao.load(Athlete.class, tempid);
		leekens = (Trainer) persdao.load(Trainer.class, temptrain);
		builder.append("\n\n\n" + jef + "\n");
		builder.append(leekens + "\n");

		// test deletion of linked trainer

		persdao.delete(leekens);
		jef = (Athlete) persdao.load(Athlete.class, tempid);
		leekens = (Trainer) persdao.load(Trainer.class, temptrain);
		builder.append("\n\n\n" + jef + "\n");

		builder.append("\n" + memorial.getName());
		builder.append("\n " + memorial.getEdition());
		builder.append("\n"
				+ memorial.getPerformances().iterator().next().getResult()
						.getTime());
		resp.setContentType("text/plain");
		resp.getWriter().println(builder.toString());
	}
}
